﻿// License: The MIT License (MIT) Copyright (c) 2011..2012 Barend Gehrels

// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Data;
using Stormy;

namespace GrooveSharp
{
    public class TrackMapper : ISelectable<Track>, IInsertable<Track>
    {
        public Track ApplySelect(IDataReader reader)
        {
            Track track = new Track();
            
            track.Id = reader.AsInteger("track_id").Value;

            track.Title = reader.AsString("track_title");

            track.PathId = reader.AsInteger("path_id").Value;
            track.FileSize = reader.AsInteger("file_size");
            track.TrackNumber = reader.AsInteger("track_number");
            track.Duration = reader.AsTimeSpan("track_duration");
            track.FolderName = reader.AsString("folder_name");
            track.FileName = reader.AsString("file_name");
            track.Year = reader.AsInteger("track_year");
            track.ItunesPersistentId = reader.AsString("itunes_id");
            track.IsDeleted = reader.AsBool("is_deleted", false);

            return track;
        }

        public void ApplyInsert(Track track, IDbCommand command)
        {
            command.Parameters.Add(Db.CreateParameter(command, DbType.String, "@tt", track.Title));
            command.Parameters.Add(Db.CreateParameter(command, DbType.String, "@tst", track.SortTitle));
            command.Parameters.Add(Db.CreateParameter(command, DbType.String, "@fn", track.FolderName));
            command.Parameters.Add(Db.CreateParameter(command, DbType.String, "@filen", track.FileName));
            command.Parameters.Add(Db.CreateParameter(command, DbType.Int32, "@filesize", track.FileSize));
            command.Parameters.Add(Db.CreateParameter(command, DbType.Int32, "@year", track.Year));

            command.Parameters.Add(Db.CreateParameter(command, DbType.Int32, "@path", track.PathId));
            command.Parameters.Add(Db.CreateParameter(command, DbType.Int32, "@album", track.AlbumId));
            command.Parameters.Add(Db.CreateParameter(command, DbType.Int32, "@artist", track.ArtistId));
            command.Parameters.Add(Db.CreateParameter(command, DbType.Int32, "@disc", track.DiscNumber));
            command.Parameters.Add(Db.CreateParameter(command, DbType.Int32, "@track", track.TrackNumber));
            command.Parameters.Add(Db.CreateParameter(command, DbType.Int32, "@trackcount", track.TrackCount));
            command.Parameters.Add(Db.CreateParameter(command, DbType.String, "@duration", track.Duration.ToString()));
        }

        public string InsertSql()
        {
            return
                @"insert into tracks
                    (
                        track_title, track_sorttitle, track_year, folder_name, file_name, file_size, artist_id, path_id, album_id, disc_number, track_number, track_count, track_duration
                    )
                    values
                    (
                        @tt, @tst, @year, @fn, @filen, @filesize, @artist, @path, @album, @disc, @track, @trackcount, @duration
                    )";
        }
    }

}
